package com.xxl.job.admin.core.util;

import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.dao.XxlJobGroupDao;

import javax.annotation.Resource;
import java.util.*;

public class TriggerChoose {
    @Resource
    private static XxlJobGroupDao xxlJobGroupDao;



    //public int triggerAuto(int CPU){
    public int triggerAuto(String SourceType, String CPU, String Memory){
        //System.out.println(SourceType);
        int groupID = 1;
        //任务资源需求 task
        int[] task = new int[5];

        List<XxlJobGroup> xxlJobGroups = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().findAll();
        //List<XxlJobGroup> xxlJobGroups = xxlJobGroupDao.findAll();
        if(SourceType.indexOf("FPGA")!=-1) task[0] = 1;
        if (SourceType.indexOf("GPU")!=-1) task[1] = 1;
        if (SourceType.indexOf("NPU")!=-1) task[2] = 1;
        if (SourceType.indexOf("CPU")!=-1) task[3] = 1;
        //int cpu = Integer.parseInt(CPU);
        int memory = Integer.parseInt(Memory);

        //[3] = cpu;
        task[4] = memory;


        //group资源map
        HashMap<Integer, int[]> map = new HashMap<>();
        for(int i=1;i<xxlJobGroups.size();i++){

            int[] temp0 = new int[5];
            temp0[0] = xxlJobGroups.get(i).getFPGA();
            temp0[1] = xxlJobGroups.get(i).getGPU();
            temp0[2] = xxlJobGroups.get(i).getNPU();
            temp0[3] = xxlJobGroups.get(i).getCPU();
            //System.out.println("id"+xxlJobGroups.get(i).getId()+"cpu"+xxlJobGroups.get(i).getCPU());
            temp0[4] = xxlJobGroups.get(i).getMemory();

            map.put(xxlJobGroups.get(i).getId(),temp0);
        }
        //System.out.println(map.entrySet().size());
        //map数组
        Set set = map.entrySet();
        Map.Entry[] entries = (Map.Entry[])set.toArray(new Map.Entry[set.size()]);



        int id = 1;
        ArrayList<Integer> Id = new ArrayList<>();
        for (Map.Entry<Integer, int[]> entry : map.entrySet()) {
            int i = 0;
            int temp = 0;
            XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(entry.getKey());

            for (;  i < 4; i++) {
                //System.out.println("任务对比");
                //System.out.println(task[i]+" "+entry.getValue()[i]);
                if (task[i]==1 && entry.getValue()[i]>0) {
                    temp=1;
                }
            }
            if(temp != 0){
                for (i=4;  i < 5; i++) {
                    //System.out.println("任务对比");
                    //System.out.println(task[i]+" "+entry.getValue()[i]);
                    if (task[i] > entry.getValue()[i]) {
                        break;
                    }
                }
                if (i > 3 && group.getAddressList()!=null) {

                    Id.add(entry.getKey());
                    //System.out.println(entry.getKey());
                    //id = entry.getKey();
                    //break;

                }
            }

        }
        if(Id.size()>0){
            int num = (int)(Math.random()*(Id.size()));
            id = Id.get(num);

        }




        //最终
        System.out.println(id);
        if (id == 1) {
            System.out.println("失败"+id);
            return 1;
        }

        /*int[] temp = map.get(id);
        for (int j = 0; j < temp.length; j++) {
            temp[j] -= task[j];
        }
        map.put(id, temp);

        XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(id);
        group.setFPGA(temp[0]);
        group.setGPU(temp[1]);
        group.setNPU(temp[2]);
        group.setCPU(temp[3]);
        group.setMemory(temp[4]);
        XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().update(group);
        System.out.println("资源占用");*/
        return id;
    }

//    public static void main(String[] args){
//        String str = "GPU,FPGA";
//        //System.out.println(str.indexOf("NPU"));
//        ArrayList<Integer> Id = new ArrayList<>();
//        Id.add(1);
//        Id.add(2);
//        Id.add(3);
//        int num = (int)(Math.random()*(1));
//        System.out.println(Id.size()+" "+num);
//
//    }
}
